<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}

$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
	include "../value/value_en.php";
} else if($lang == "VN") {
	include "../value/value_vn.php";
}
else {
	include "../value/value_kr.php";
}
include '../operations/connection.php';	

$groupAssets=$_REQUEST['groupAssets'];
$typeAssets=$_REQUEST['typeAssets'];
$nameAssets=rawurldecode($_REQUEST['nameAssets']);


$sql1="AND c.`name` LIKE '%".$nameAssets."%'";
if($groupAssets !=0){
	$sql1.=" AND c.group='".$groupAssets."'";
}
if($typeAssets !=0){
	$sql1.=" AND c.type='".$typeAssets."'";
}
if($_REQUEST['using']==1)
	$sql1.=" AND c.count!='0'";
if($_REQUEST['stock']==1)
	$sql1.=" AND c.stock!='0'";
if($_REQUEST['notuse']==1)
	$sql1.=" AND c.notuse!='0'";
	

	
$sql="SELECT g.`name` as nameg,b.*,(SELECT count(*) FROM assets c WHERE c.`group`=b.`group` AND c.type=b.type ".$sql1.") as counttype ,
									(SELECT SUM(c.count+c.notuse+c.stock) FROM assets c WHERE c.`group`=b.`group` ".$sql1.") as sumassets ,	
									(SELECT e.FullName FROM employee e WHERE e.IDEmployee=b.creater) as FullName
FROM group_assets g LEFT JOIN (SELECT a.*,t.`name` as namet FROM assets a , type_assets t WHERE a.type=t.id ) b  ON b.`group`=g.id 
WHERE b.`name` LIKE '%".$nameAssets."%' ";

if($groupAssets !=0){
	$sql.=" AND b.group='".$groupAssets."'";
}
if($typeAssets !=0){
	$sql.=" AND b.type='".$typeAssets."'";
}
if($_REQUEST['using']==1)
	$sql.=" AND b.count!='0'";
if($_REQUEST['stock']==1)
	$sql.=" AND b.stock!='0'";
if($_REQUEST['notuse']==1)
	$sql.=" AND b.notuse!='0'";
	
$sql.=" ORDER BY g.`name`,b.namet,b.`name`;";

$_SESSION['searchstrAssets']=$sql;
$_SESSION['searchsqlAssets']=$sql1;
$result=mysql_query($sql);
$i=0;
$assets1=array();
$assets=array();
while ($row=mysql_fetch_array($result)){
	$i++;
	$assets1[$row['nameg']]=0;
	$assets[$row['nameg']][]=array($row['name'],$row['count'],$row['notuse'],$row['stock'],$row['namet'],$row['note'],$row['counttype'],$row['FullName'],$row['datetime'],$row['id'],"table-row","none",$row['type'],$row['group'],$row['sumassets'],$row['value'],$row['place']);
}
$_SESSION['tassets1']=$assets1;
$_SESSION['tassets']=$assets;
?>
<table cellpadding="0" cellspacing="1" border="0" class="ftable1" style="padding-left: 10px">
				            <tr>
				            	<th style="width: 150px"><?php echo $l_groupassets;?></th>
				            	<th style="width: 150px"><?=$l_typeassets?></th>
				            	<th style="width: 30px"><?php echo $l_no;?></th>
				            	<th style="width: 30px">
				            		<a onclick="deletesAssets('<?=$i?>')"><img src="images/del.png"  title="<?php echo $l_delete;?>" /></a>
								</th>
				                <th style="width: 200px"><?php echo $l_nameassets;?></th>
				                
				                <th style="width: 60px"><?php echo $l_total;?></th>
				                <th style="width: 60px"><?php echo $l_using;?></th>
				                <th style="width: 60px"><?php echo $l_notused;?></th>
				                <th style="width: 60px"><?php echo $l_stock;?></th>
				                
				                <th style="width: 130px"><?php echo $l_value; ?></th>
                                <th style="width: 220px"><?php echo $l_place; ?></th>
				                
				                <th style="width: 140px"><?php echo $l_note;?></th>
				                 <th style="width: 20px"><?php echo $l_memo; ?></th>
				                <th style="width: 140px"><?php echo $l_author;?></th>
				                <th style="width: 140px"><?php echo $l_time;?></th>
				                <th style="width: 20px"><?php echo $l_move;?></th>
				            </tr>
				            <?php 
						            $i=0;
						foreach ($assets as $value => $giatri) {
							
						
							if($giatri[0][0]=="")
								echo '<tr>
						                <td colspan="16" style="background:#D0D8E8;">'.$value.'(0)</td></tr>';
							else{
								$quey=mysql_query("SELECT count(DISTINCT a.type) as count FROM assets a WHERE a.group='".$giatri[0][13]."' ");
								$r1=mysql_fetch_array($quey);
								echo '<tr style="display: '.$giatri[0][11].';" >
						               <td colspan=16 style="background:#D0D8E8; vertical-align: top;padding-top: 8px; padding-left: 10px;font-size: ;text-align: left;" ><img src="images/arrow-down.png" style=";" onclick="showsearchAssets(\''.$value.'\',0)"/><span style="padding-left: 5px;vertical-align: top;">'.$value.' ('.$giatri[0][14].')</span></td>
						             </tr>';
							echo '<tr style="display: '.$giatri[0][10].';">
						                <td style="background:#D0D8E8; vertical-align: top;padding-top: 8px; padding-left: 10px;font-size: ;text-align: left;" rowspan="'.count($giatri).'"><img src="images/arrow-up.png" style=";" onclick="showsearchAssets(\''.$value.'\',1)"/><span style="padding-left: 5px;vertical-align: top;">'.$value.' ('.$giatri[0][14].')</span></td>';
							$j=0;
							$temp="";
							foreach ($giatri as $value1 => $giatri1) {
								$i++;
								$j++;
								if($j!=1)
									echo '<tr style="display: '.$giatri1[10].';">';
									
									if($temp!=$giatri1[4]){
										
										$result=mysql_query("SELECT SUM(c.count) as sumcount,SUM(c.notuse) as sumnotuse,SUM(c.stock) as sumstock FROM assets c WHERE c.type='".$giatri1[12]."' AND c.`group`='".$giatri1[13]."' ".$sql1.";");
				            			$temp=	$giatri[5];
				            			$r=mysql_fetch_assoc($result);
				            			$tong=$r['sumcount']+$r['sumnotuse']+$r['sumstock'];
										
										echo '  <td style="background:#D0D8E8;vertical-align: top;padding-top: 8px;text-align: left;font-size: ;padding-left: 10px;" rowspan="'.$giatri1[6].'">'.$giatri1[4].'('.$tong.')</td>';
										$temp=$giatri1[4];
									}
									$style="";
									//if((strtotime('now')-strtotime($giatri1[8]))<86400) 
									// 		$style="background: #FF9999";
									echo '		<td style="font-size: ;text-align: center;'.$style.'">'.$i.'</td>';
									echo '		<td style="font-size: ;text-align: center;'.$style.'"><input type="checkbox" name="checksis'.$i.'" id="checksis'.$i.'"  value="'.$giatri1[9].'"></td>';
									echo '      <td style="text-align: left;font-size: ;padding-left: 10px;'.$style.'"><a onclick="checkonlysis('.$giatri1[9].')">'.$giatri1[0].'</a></td>';
								              
									echo          ' <td style="text-align:center;font-size: ;font-weight: bold;'.$style.'">'.($giatri1[1]+$giatri1[2]+$giatri1[3]).'</td>
													<td style="text-align:center;font-size: ;'.$style.'">'.$giatri1[1].'</td>';
								
									echo          ' <td style="text-align:center;font-size: ;'.$style.'">'.$giatri1[2].'</td>';
								
									echo          ' <td style="text-align:center;font-size: ;'.$style.'">'.$giatri1[3].'</td>';
								                
									echo      	  ' <td style="text-align: right;padding-right: 5px;font-size: ;'.$style.'">'.number_format($giatri1[15]).'</td>
													<td style="text-align: left;padding-left: 5px;font-size: ;'.$style.'">'.$giatri1[16].'</td>
													<td style="text-align: left;padding-left: 5px;font-size: ;'.$style.'">'.$giatri1[5].'</td>
													<td><a onclick="showmemoAssets(\''.$giatri1[9].'\',\''.$giatri1[0].'\',this)"><img src="images/attibutes.png"  title="'.$l_memo.'" /></a></td>
													<td style="text-align:center;font-size: ;'.$style.'">'.$giatri1[7].'</td>
													<td style="text-align:center;font-size: ;'.$style.'">'.$giatri1[8].'</td>
													<td style="text-align:center;font-size: ;'.$style.'"><a onclick="showmoveAssets(\''.$giatri1[9].'\')"><img src="images/communication.png"  title="'.$l_move.'" /></a></td>
								                </tr>';
							}
						
						}
				            	?>
				            <!--  <tr>
				             	<td><?php echo $i;?></td>
				             	<td style="text-align: left;"><?php echo $r['name'];?></td>
				             	 <?php if($_REQUEST['using']==1){?>
				             	<td><?php echo $r['count'];?></td>
				             	 <?php }if($_REQUEST['notuse']==1){?>
				             	<td><?php echo $r['notuse'];?></td>
				             	 <?php }if($_REQUEST['stock']==1){?>
				             	<td><?php echo $r['stock'];?></td>
				             	<?php }?>
				             	<td style="text-align: left;"><?php echo $r['note'];?></td>
				             </tr> -->
				           <?php }
				           if($i==0){
				           ?>
				           <tr><td colspan="15" style="height:30px;text-align:left;padding-left:20px;"><?php echo $l_list_emplty;?></td></tr>
				           <?php }?>
			   </table>
<?php 
mysql_close($connection);
?>